#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H

#include "core_global.h"
#include <boost/noncopyable.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/function.hpp>

class TcpConnection : protected boost::noncopyable
{
public:
    typedef boost::asio::io_service ServiceImpl;
    typedef boost::asio::ip::tcp::socket Socket;
    typedef boost::asio::ip::tcp::endpoint Endpoint;
    typedef boost::system::error_code ErrorCode;

    typedef boost::function< size_t (size_t) > FilterHandler;
    typedef boost::function< void (size_t, int) > CompleteHandler;

public:
    TcpConnection(ServiceImpl &impl);
    ~TcpConnection();

    Socket &sock();

    void close();

    void read(void *buf, size_t size,
              BOOST_ASIO_MOVE_ARG(FilterHandler) filter,
              BOOST_ASIO_MOVE_ARG(CompleteHandler) handler);

    void write(const void *buf, size_t size,
               BOOST_ASIO_MOVE_ARG(FilterHandler) filter,
               BOOST_ASIO_MOVE_ARG(CompleteHandler) handler);

protected:
    size_t isComplete(ErrorCode ec, size_t size, FilterHandler filter);

    void onComplete(ErrorCode ec, size_t size, CompleteHandler handler);

private:
    Socket m_sock;
};

#endif // TCPCONNECTION_H
